<template>
  <div>
    <el-dialog
      :visible="addDialog"
      width="50%"
      :before-close="closeDialog"
      top="5vh"
      :close-on-click-modal="false"
    >
      <div class="dialogTitle" slot="title">
        {{ this.form.id ? "编辑消防" : "新增消防" }}
      </div>
      <el-form :model="form" label-width="180px">
        <el-form-item label="消防设备编码:">
          <el-input
            v-model="form.equipmentCode"
            autocomplete="off"
            placeholder="请输入"
          />
        </el-form-item>

        <el-form-item label="消防设备名称:">
          <el-input
            v-model="form.equipmentName"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
        <el-form-item label="位置:">
          <el-input
            v-model="form.address"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
        <el-form-item label="状态:">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="item in options1"
              :key="item.label"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="检查时间:">
          <el-date-picker
            v-model="form.checkDate"
            type="date"
            placeholder="选择检查时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="检查人:">
          <el-input
            v-model="form.checkerName"
            autocomplete="off"
            placeholder="请输入 "
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="!form.id" class="addBtn" type="primary" @click="submit"
          >确定新增</el-button
        >
        <el-button v-else class="addBtn" type="primary" @click="submit"
          >确定修改</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fireAddApi, fireUpdateApi } from "@/api/fireTable";

export default {
  name: "addFire",
  props: {
    addDialog: {
      type: Boolean,
      default: false,
    },
    dialogId: {
      type: String,
      default: null,
    },
  },
  components: {},
  data() {
    return {
      form: {
        equipmentCode: "",
        equipmentName: "",
        address: "",
        status: "",
        checkDate: "",
        checkerName: "",
      },
      options1: [
        {
          value: 0,
          label: "正常",
        },
        {
          value: 1,
          label: "异常",
        },
      ],
    };
  },
  created() {},
  methods: {
    async submit() {
      if (this.form.id) {
        const res = await fireUpdateApi({
          id: this.form.id,
          equipmentCode: this.form.equipmentCode,
          equipmentName: this.form.equipmentName,
          address: this.form.address,
          status: this.form.status,
          checkDate: this.form.checkDate,
          checkerName: this.form.checkerName,
        });

        if (res.code == 0) {
          this.$message.success("修改成功");
          this.closeDialog();
          this.$parent.firePage();
        } else {
          this.$message.error("新增失败");
        }
      } else {
        const res = await fireAddApi({
          equipmentCode: this.form.equipmentCode,
          equipmentName: this.form.equipmentName,
          address: this.form.address,
          status: this.form.status,
          checkDate: this.form.checkDate,
          checkerName: this.form.checkerName,
        });

        if (res.code == 0) {
          this.$message.success("新增成功");
          this.closeDialog();
          this.$parent.firePage();
        } else {
          this.$message.error("新增失败");
        }
      }
    },
    closeDialog() {
      this.form = {
        equipmentCode: "",
        equipmentName: "",
        address: "",
        status: "",
        checkDate: "",
        checkerName: "",
      };

      this.$emit("update:addDialog", false); // 触发父组件更新
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
};
</script>

<style scoped lang="scss">
.dialogTitle {
  position: relative;
  padding-left: 10px; /* 调整文本与颜色条之间的间距 */
  font-weight: bold;
}
.dialogTitle::before {
  content: "";
  position: absolute;
  left: 0;
  top: 50%;
  border-radius: 2px;
  transform: translateY(-50%);
  width: 4px; /* 颜色条的宽度 */
  height: 14px; /* 颜色条的高度 */
  background-color: #249451; /* 颜色条的颜色 */
}
.el-select {
  width: 100%;
}
::v-deep .el-dialog__body {
  padding-right: 50px !important;
}
::v-deep .dialog-footer {
  text-align: center;
  .addBtn {
    background: #249451;
    border-radius: 4px 4px 4px 4px;
    border: none;
    color: #fff;
  }
}

::v-deep .el-upload--picture-card {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
::v-deep .el-upload-list--picture-card .el-upload-list__item-actions {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
::v-deep .el-upload-list--picture-card .el-upload-list__item {
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
</style>
